#include "StdAfx.h"
#include "FIT_6204_Gorodilov_Collection.h"

CFIT_6204_Gorodilov_Collection::CFIT_6204_Gorodilov_Collection(void)
{
}

CFIT_6204_Gorodilov_Collection::~CFIT_6204_Gorodilov_Collection(void)
{
}


BOOL CFIT_6204_Gorodilov_Collection::addPoint(const CFIT_6204_Gorodilov_Point point) {
	if (arr.IsEmpty()) {
		arr.Add(point);
		return TRUE;
	}
	INT_PTR left = 0;
	INT_PTR right = arr.GetCount() - 1;
	CFIT_6204_Gorodilov_Point p = arr.GetAt((left + right) / 2);
	if (0 >= comparePoints(&point, &arr.GetAt(left))) {
		arr.InsertAt(0, point);
		return TRUE;
	}
	if (0 <= comparePoints(&point, &arr.GetAt(right))) {
		arr.Add(point);
		return TRUE;
	}
	while (right - left> 1) {
		int res = comparePoints(&point, &p); 
		if (-1 == res) { // point < p
			right = (left + right) / 2;
		} else {
			if (0 == res) {
				return FALSE;
			} else {
				left = (left + right) / 2;
			}
		}
		p = arr.GetAt((left + right) / 2);
	}
	arr.InsertAt(left + 1, point);
	return TRUE;
}

void CFIT_6204_Gorodilov_Collection::pop(void) {
	if (!arr.IsEmpty()) {
		arr.RemoveAt(0);
	}
}

CFIT_6204_Gorodilov_Point CFIT_6204_Gorodilov_Collection::getMin(void) {
	if (!arr.IsEmpty()) {
		return arr.GetAt(0);
	} else {
		return CFIT_6204_Gorodilov_Point();
	}
}